package com.zl.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zl.entity.BrowseEntity;
import com.zl.entity.Collect;
import com.zl.entity.Details;
import com.zl.service.BorwseService;
import com.zl.service.CollectService;
import com.zl.service.NewsService;
import com.zl.util.PropertiesHelper;
import com.zl.weixin.entity.UserClient;

/** 
* @ClassName: DetailController 
* @Description: 详情
* @author chxl
* @date 2015-4-30 上午09:24:10 
*  
*/
@Controller
@RequestMapping("/detail")
@Scope("prototype")
public class DetailController {
	PropertiesHelper properties = new PropertiesHelper("app.properties");
	Logger log = Logger.getLogger(DetailController.class);
	@Autowired
	private BorwseService borwseService;
	@Autowired
	private CollectService collectService;
	@Autowired
	private NewsService newsService;
	/**
	 *  详情页面 保存浏览记录
	 * @return
	 */
	@RequestMapping("/index")
	public String index(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		
		HttpSession session = request.getSession();
		UserClient user = (UserClient)session.getAttribute("user");
//		UserClient user = new UserClient();
//		user.setOpenid("oFOgks6AbGnBns_u2teOtiMG711c");
		if(user==null){
			return "";
		}
		/*
		 * 保存浏览记录
		 */
		borwseService.insertBrowse(user.getOpenid(), id,type);
		List<BrowseEntity> browse = borwseService.selectBorwseCount(id);
		if(browse==null||browse.size()==0){
			borwseService.insertBorwse(id, UUID.randomUUID().toString());
			model.addAttribute("browseCount",1);
		}else{
			borwseService.updateBorwse(id);
			model.addAttribute("browseCount",Integer.parseInt(browse.get(0).getCount())+1);
		}
		Details detailsq = new Details();
		detailsq.setId(id);
		detailsq.setTableName(type);
		Details details = newsService.selectDetailById(detailsq);
		String ym = details.getContentWithHTML();
		details.setContentWithHTML(repalceYm(ym));
		model.addAttribute("details", details);
		
		return "con_zb";
		
	}
	
	@RequestMapping("/indexSub")
	public String indexSub(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		
		HttpSession session = request.getSession();
		UserClient user = (UserClient)session.getAttribute("user");
//		UserClient user = new UserClient();
//		user.setOpenid("oFOgks6AbGnBns_u2teOtiMG711c");
		if(user==null){
			return "";
		}
		/*
		 * 保存浏览记录
		 */
		borwseService.insertBrowse(user.getOpenid(), id,type);
		List<BrowseEntity> browse = borwseService.selectBorwseCount(id);
		if(browse==null||browse.size()==0){
			borwseService.insertBorwse(id, UUID.randomUUID().toString());
			model.addAttribute("browseCount",1);
		}else{
			borwseService.updateBorwse(id);
			model.addAttribute("browseCount",Integer.parseInt(browse.get(0).getCount())+1);
		}
		Details detailsq = new Details();
		detailsq.setId(id);
		detailsq.setTableName(type);
		Details details = newsService.selectDetailById(detailsq);
		String ym = details.getContentWithHTML();
		details.setContentWithHTML(repalceYm(ym));
		model.addAttribute("details", details);
		
		return "con_zb_sub";
		
	}
	
	public static String repalceYm(String ym){
		if(StringUtils.isNotBlank(ym)){
			if(ym.indexOf("<!--")>-1){
				String replaceStr = ym.substring(ym.indexOf("<!--"),ym.indexOf("-->")+3);
				//System.out.println(replaceStr);
				ym = ym.replace(replaceStr, "");
				if(ym.indexOf("<!--")>-1){
					ym = repalceYm(ym);
				}
			}
		}
		return ym;
	}
}
